<html>
    <head>
        <title>IoT City Test Portal</title>
        <link rel="shortcut icon" href="{{ url_for('static', filename='favicon.ico') }}">
        <link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">
  
        <script src="https://code.jquery.com/jquery-3.1.1.min.js"></script>
        <script src="https://code.highcharts.com/stock/highstock.js"></script>
        <script src="https://code.highcharts.com/stock/modules/exporting.js"></script>

        <script src="https://code.jquery.com/jquery-1.12.4.js"></script>
        <script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
  
        <script src="{{ url_for("static", filename="ws.js") }}"></script>
        
        <link href='http://fonts.googleapis.com/css?family=PT+Sans:400,700,400italic' rel='stylesheet' type='text/css'>
        <link href='{{ url_for("static", filename="style.css") }}' rel='stylesheet' type='text/css'>
    </head>
    <body>
        <script type="text/javascript">
            var service_url="http://localhost:8000/authenticated";
            var token=localStorage.getItem('access_token');

            var xhr = new XMLHttpRequest();
            xhr.onreadystatechange = function() {
                if (xhr.readyState == 4) {
                    if (xhr.status != 200) {
                        window.location.replace("/login");
                        return false;
                    }
                    return true;
                }
            }
            xhr.open("GET", service_url, true);
            xhr.setRequestHeader('Authorization', 'Bearer ' + token);
            xhr.send();
        </script>

        <div class="sidenav">
            <a href="/">Home</a>
            <a href="/verticals">Verticals</a>
            <a href="/devices">Devices</a>
            <a href="/subscriptions">Subscriptions</a>
            <a href="/alerts">Alerts</a>
            <a href="/triggered">Triggered</a>
        </div>

        <div body style="margin-left: 200px;">

            <table id="alerts">
                <tr>
                    <td>ID</td>
                    <td>Subscription ID</td>
                    <td>Alarm Type</td>
                    <td>Threshold</td>
                    <td>Actuators</td>
                    <td>Delete</td>
                </tr>
            </table>

            <button id="create-alert">Create new alert</button>
        </div>

        <div id="dialog-form" title="Add Alert">
         
          <form>
            <fieldset>
              <label for="subscription_id">Subscription ID</label>
              <input type="text" name="subscription_id" id="subscription_id" value="" class="text ui-widget-content ui-corner-all"/><br>

              <label for="alarm_type">Alarm Type (MAX, MIN)</label><br>
              <input type="text" name="alarm_type" id="alarm_type" value="" class="text ui-widget-content ui-corner-all"><br>
         
              <label for="threshold">Threshold</label><br>
              <input type="text" name="threshold" id="threshold" value="" class="text ui-widget-content ui-corner-all"><br>
              <input type="submit" tabindex="-1" style="position:absolute; top:-1000px">
            </fieldset>
          </form>
        </div>

        <script type="text/javascript">
            var service_url="http://localhost:8000"
            var url = service_url+'/alerts';
            $.ajax({ url: url,
                headers: {'Authorization': 'Bearer ' + localStorage.getItem('access_token')},
                success: function(data) {
                    values = data;
                    var table = document.getElementById("alerts");
                    for(value in values){
                        element = values[value];
                        
                        var button = document.createElement("button");
                        (function(id){
                            button.addEventListener('click', function(){ deleteAlert(id); }, true);
                        })(element["id"]);
                        button.innerHTML = "Delete";

                        var row = table.insertRow(-1);
                        var cell1 = row.insertCell(0);
                        var cell2 = row.insertCell(1);
                        var cell3 = row.insertCell(2);
                        var cell4 = row.insertCell(3);
                        var cell5 = row.insertCell(4);
                        var cell6 = row.insertCell(5);

                        var button2 = document.createElement("button");
                        (function(id){
                            button2.addEventListener('click', function(){ document.location.href = "/alert/"+id+"/actuators"; }, true);
                        })(element["id"]);
                        button2.innerHTML = "Actuators";
                            
                        cell1.innerHTML = element["id"];
                        cell2.innerHTML = element["subscription_id"];
                        cell3.innerHTML = element["alarm_type"];
                        cell4.innerHTML = element["threshold"];
                        cell5.appendChild(button2);
                        cell6.appendChild(button);
                    }
                },
                error: function(XMLHttpRequest, textStatus, errorThrown) {
                }
            });

            function deleteAlert(id){
                var xhr = new XMLHttpRequest();
                xhr.open("DELETE", service_url+"/alert/"+id, true);
                xhr.setRequestHeader('Content-Type', 'application/json; charset=UTF-8');
                xhr.setRequestHeader('Authorization', 'Bearer ' + localStorage.getItem('access_token'));
                xhr.send();
                xhr.onloadend = function () {
                    location.reload();
                };
            }

            $( function() {
                var dialog, form;
                
                function addAlert() {
                    var xhr = new XMLHttpRequest();
                    data = {"subscription_id": document.getElementById("subscription_id").value, "alarm_type": document.getElementById("alarm_type").value, "threshold": document.getElementById("threshold").value};
                    xhr.open("POST", service_url+"/alerts", true);
                    xhr.setRequestHeader('Content-Type', 'application/json; charset=UTF-8');
                    xhr.setRequestHeader('Authorization', 'Bearer ' + localStorage.getItem('access_token'));
                    xhr.send(JSON.stringify(data));
                    xhr.onloadend = function () {
                        window.location.reload();
                    };
                }
            
                dialog = $( "#dialog-form" ).dialog({
                    autoOpen: false,
                    height: 400,
                    width: 350,
                    modal: true,
                    buttons: {
                        "Add Alert": addAlert,
                        Cancel: function() {
                            dialog.dialog( "close" );
                        }
                    },
                    close: function() {
                    form[ 0 ].reset();
                    }
                });
             
                form = dialog.find( "form" ).on( "submit", function( event ) {
                    event.preventDefault();
                    addStream();
                });
             
                $( "#create-alert" ).button().on( "click", function() {
                    dialog.dialog( "open" );
                });
            });

        </script>

    </body>
</html>

